package com.dcjt.firehotcarpad.utils;

import com.aries.library.fast.retrofit.FastMultiUrl;
import com.aries.library.fast.util.SPUtil;
import com.aries.library.fast.util.ToastUtil;
import com.dcjt.firehotcarpad.base.ApiPath;
import com.dcjt.firehotcarpad.base.App;
import com.dcjt.firehotcarpad.base.SPConstant;
import com.dcjt.firehotcarpad.entity.AppConfig;
import com.dcjt.firehotcarpad.entity.BaseEntity;
import com.dcjt.firehotcarpad.entity.CarEntity;
import com.dcjt.firehotcarpad.entity.LoginResultEntity1;
import com.dcjt.firehotcarpad.entity.UserEntity;
import com.dcjt.firehotcarpad.manager.DataManager;
import com.google.gson.Gson;
import com.juntu.loginlib.contact.LoginConstant;

/**
 * Created by cj on 2021/2/19.
 * Email:codesexy@163.com
 * Function:
 * desc:
 */
public class LoginUtil {

    /**
     * 对配置信息做统一的处理
     * @param appConfig
     */
    public static void parseConfigData(String appConfig) {
        AppConfig config = new Gson().fromJson(appConfig, AppConfig.class);
        DataManager.getInstance().setAppConfigEntity(config);

        String url_a = "";
        String pmqUrl = config.getAppServerUrl().getPmqUrl();
//        var pmqUrl = if (StringUtils.isEmpty(DataManager.getInstance().configEntity.pmq?.pmqUrl_1)) DataManager.getInstance().configEntity.pmq?.pmqUrl else DataManager.getInstance().configEntity.pmq?.pmqUrl_1                        //获取消息应用信息
        String[] path =  pmqUrl.split(":");

        if(path.length >2){
            if ( path[2].contains("/")){
                url_a =   path[2].substring(path[2].lastIndexOf("/")+1);
            }
        }


        SPUtil.put(App.get(), SPConstant.MESSAGE_URL_PORT, url_a);
        SPUtil.put(App.get(), SPConstant.FILE_UPLOAD_URL, config.getAppServerUrl().getLogUrl());

        FastMultiUrl.getInstance().putBaseUrl(
                ApiPath.KEY_BASE_MESSAGE,
                pmqUrl
        ).putBaseUrl(
                ApiPath.KEY_BASE_UPLOAD_FILE,
                config.getAppServerUrl().getLogUrl()
        );



    }


    public static void parseLoginResult(String loginResult) {
        //测试闪退问题
//        String str = "你好1111";
//        double a = Double.parseDouble(str);
        BaseEntity result1 = new Gson().fromJson(loginResult, BaseEntity.class);
        if (result1.code == 200){
            LoginResultEntity1 result = new Gson().fromJson(loginResult, LoginResultEntity1.class);
            UserEntity userEntity = new UserEntity(result.getData().getId(),result.getData().getAccount()
                    ,result.getData().getReal_name(),result.getData().getDept_id(),result.getData().getDlzdlx(),
                    result.getData().getDept_name(),result.getData().getJgxzdm(),result.getData().getXzqdm()
                    ,result.getData().getX(),result.getData().getY()
//                    ,"104.0604881049515","30.574762622548917"
//                    ,"104.060688","30.57434232"
                    ,result.getData().getCf_ryjbxx_id(),result.getData().getDxlb(),
                    result.getData().getToken(),result.getData().getJbxx().getCphm(),result.getData().getJbxx().getCllxdm() );
            DataManager.getInstance().saveUser(userEntity);
            DataManager.getInstance().setPmqBean(result.getData().getPmq());
            SPUtil.put(App.get(), SPConstant.CAR_CONFIG_NAME,userEntity.getDept_name()+"-"+ StringUtils.getNoNullString(result.getData().getJbxx().getClmc(),"")+"-"+  StringUtils.getNoNullString(result.getData().getJbxx().getCphm(),""));
            DataManager.getInstance().setGpsbh("7"+StringUtils.getNoNullString(result.getData().getJbxx().getGpsbh(),""));
        } else {
            ToastUtil.show(result1.msg);
        }

        }



}
